<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/semantic-ui/2.2.4/semantic.min.css">
<script src="../js/jquery.1.9.min.js"></script>
<script src="https://cdn.bootcss.com/semantic-ui/2.2.4/semantic.js"></script>

<script>
    $(function () {
        load(null);

        $("#addTag").on("click", function () {
            $("#viewShow1").modal("show");
        });

        $('body').on('click', '#updateTag', function () {

            $("#viewShow").modal("show");
            $("#updateTagName").val($(this).data("value"));
            var tagName1 = $(this).data("value");
            $("#update").click(function () {
                var tagName = $("#updateTagName").val();
                $.post("/wenote/tag?method=updateTagName", {tagName1: tagName1, tagName: tagName}, function (data) {
                    if (data.flag) {
                        alert("修改成功");
                        location.reload();
                    } else {
                        alert(data.errorMsg);
                    }
                });
            });
        });

        $('body').on('click', '#delete', function () {
            var tagName = $(this).data("value");
            if (confirm("您确定要删除吗？")) {
                $.post("/wenote/tag?method=deleteTagByTagName", {tagName: tagName}, function (data) {
                    if (data.flag) {
                        alert("删除成功");
                        location.reload();
                    } else {
                        alert(data.errorMsg);
                    }
                });
            }
        });


        function load(currentPage) {

            var c = "";
            $.get("/wenote/tag?method=findAllTagByPage", {currentPage: currentPage}, function (data) {
                $("#totalCount").html(data.totalCount);
                $("#totalPage").html(data.totalPage);
                var firstPage = '<a class="item" onclick="load(1)">首页</a>';

                //计算上一页的页码
                var beforeNum = data.currentPage - 1;
                if (beforeNum <= 0) {
                    beforeNum = 1;
                }

                var beforePage = '<a class="item" data-name="" onclick="load(' + beforeNum + ')">上一页</a>';

                c += firstPage;
                c += beforePage;

                /*
                    1.一共展示10个页码，能够达到前5后4的效果
                    2.如果前边不够5个，后边补齐10个
                    3.如果后边不足4个，前边补齐10个
                */

                // 定义开始位置begin,结束位置 end
                var begin;
                var end;

                if (data.totalPage < 10) {
                    begin = 1;
                    end = data.totalPage;
                } else {
                    begin = data.currentPage - 5;
                    end = data.currentPage + 4;

                    if (begin < 1) {
                        begin = 1;
                        end = begin + 9;
                    }

                    if (end > data.totalPage) {
                        end = data.totalPage;
                        begin = end - 9;
                    }
                }

                for (var i = begin; i <= end; i++) {
                    var li;
                    //判断当前页码是否等于i
                    if (data.currentPage == i) {
                        li = '<a style="color: #2aabd2;" class="item" onclick="load(' + i + ')">' + i + '</a>';
                    } else {
                        //创建页码的li
                        li = '<a class="item" onclick="load(' + i + ')">' + i + '</a>';
                    }
                    c += li;
                }

                var lastPage = '<a class="item" onclick="load(' + data.totalPage + ')">尾页</a>';

                var nextNumber = currentPage + 1;
                if (nextNumber >= data.totalPage) {
                    nextNumber = data.totalPage;
                }

                var nextPage = '<a class="item" onclick="load(' + nextNumber + ')">下一页</a>';

                c += nextPage;
                c += lastPage;

                //c+='<div  class="ui padded vertical segment m-padded-tb-large"><div  class="ui middle aligned mobile reversed stackable grid"><div class="eleven wide column"><h3 class="ui header"><a style="color: #0f0f0f;" href="noteComment.html" target="_self" class="m-black">'+data.data[i].noteTitle+'</a></h3><p class="m-text">'+data.data[i].noteContent+'</p><div class="ui grid"><div class="eleven wide column"><div class="ui mini horizontal link list"><div class="item"><img src="'+data.data[i].image+'" alt="" class="ui avatar image"><div class="content"><a href="#" class="header">'+data.data[i].author+'</a></div></div><div class="item"><i class="calendar icon"></i><span>'+data.data[i].createTime+'</span></div><div class="item"><i class="eye icon"></i> <span>'+data.data[i].visit+'</span></div></div></div><div class="right aligned five wide column"><a href="#" target="_self" class="ui teal basic label m-padded-tiny m-text-thin">'+data.data[i].categoryName+'</a></div></div></div><div class="five wide column"><a href="noteComment.html" target="_self"><img src="'+data.data[i].image+'" alt="" class="ui rounded image"></a></div></div></div>';
                $("#tagPage").html(c);

                var note_lis = "";
                for (var i = 0; i < data.list.length; i++) {
                    note_lis += '<tr><td>' + data.list[i].cnt + '</td><td id="tagName">' + data.list[i].tagName + '</td><td><a id="updateTag" href="#" class="ui teal button" data-value="' + data.list[i].tagName + '">编辑</a><input type="hidden" name="_method" value="delete"/><a id="delete" href="#" class="ui red button" data-value="' + data.list[i].tagName + '">删除</a></td></tr>';
                    // function deleteTag() {
                    //     if (confirm("您确定要删除吗？")) {
                    //         var tagName = $("#tagName").val();
                    //         $.post("/wenote/tag?method=deleteTagByTagName", {tagName: tagName}, function (data) {
                    //             if (data.flag) {
                    //                 alert("删除成功");
                    //             } else {
                    //                 alert("删除失败");
                    //             }
                    //         });
                    //     }
                    // }
                }

                $("#tagList").html(note_lis);
                window.scrollTo(0, 0);
            });

        }

        function addTag() {
            var tagName = $("#addTagName").val();
            if (tagName != null && tagName.trim().length > 0) {
                $.post("/wenote/tag?method=findTagByTagName", {tagName: tagName}, function (data) {
                    if (data.flag) {
                        $.post("/wenote/tag?method=addTag", {tagName: tagName}, function (da) {
                            if (da.flag) {
                                alert("添加成功");
                            } else {
                                alert(da.errorMsg);
                            }
                        });
                    } else {
                        alert(data.errorMsg);
                    }
                });
            } else {
                alert("输入信息不能为空");
            }
        }
    })
</script>

<body>
<div style="width: 60%;margin-left: 300px;margin-top: 3%;" class="m-padding-tb-large">
    <div class="ui container">
        <table class="ui table">
            <thead>
            <tr>
                <th>数量</th>
                <th>名称</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="tagList">
            <tr>
                <td id="tagNumber">1</td>
                <td id="tagName">类型1</td>
                <td>
                    <a href="#" class="ui teal button">编辑</a>
                    <input type="hidden" name="_method" value="delete"/>
                    <a id="delete" href="#" class="ui red button">删除</a>
                </td>
            </tr>
            </tbody>
            <tfoot>
            <tr>
            <tr>
                <th colspan="3">
                    <div id="tagPage" class="ui pagination mini menu">
                        <a class="item">首页</a>
                        <a class="item">上一页</a>
                        <a class="item">1</a>
                        <a class="item">1</a>
                        <a class="item">1</a>
                        <a class="item">1</a>
                        <a class="item">1</a>
                        <a class="item">1</a>
                        <a class="item">1</a>
                        <a class="item">1</a>
                        <a class="item">1</a>
                        <a class="item">1</a>
                        <a class="item">下一页</a>
                        <a class="item">尾页</a>
                    </div>
                    共<font id="totalCount" class="ui orange header m-inline-block m-text-thin"
                           color="red">x</font>个
                    <font id="totalPage" class="ui orange header m-inline-block m-text-thin"
                          color="red">6</font>页
                    <a id="addTag" href="#" class="ui teal basic button right floated">新增</a>
                    <a href="index.html" class="ui teal basic button right floated">返回</a>
                </th>
            </tr>
            </tr>
            </tfoot>
        </table>
    </div>
</div>
<div class="ui container">
    <div id="viewShow1" class="ui modal">
        <i class="close icon"></i>
        <div class="header"><i class="tags icon"></i>标签信息</div>
        <div class="content">
            <div class="ui input">
                <input id="addTagName" type="text" placeholder="请输入标签名称">
            </div>
            <button class="ui teal basic button" onclick="addTag();">确认</button>
        </div>
    </div>
</div>

<div class="ui container">
    <div id="viewShow" class="ui modal">
        <i class="close icon"></i>
        <div class="header"><i class="tags icon"></i>标签信息</div>
        <div class="content">
            <div id="Show" class="ui input">
                <input id="updateTagName" type="text" placeholder="请输入标签名称" value="">
            </div>
            <button id="update" class="ui teal basic button">修改</button>
        </div>
    </div>
</div>

<script>
    $('.show').click(function () {
        $('.m-item').toggleClass('m-mobile-hide')
    });
    $('.ui.dropdown').dropdown();
</script>
<!--代码放置于</body>上方-->
<script>
    !function () {

        function n(n, e, t) {

            return n.getAttribute(e) || t

        }

        function e(n) {

            return document.getElementsByTagName(n)

        }

        function t() {

            var t = e("script"), o = t.length, i = t[o - 1];

            return {

                l: o, z: n(i, "zIndex", -1), o: n(i, "opacity", .5), c: n(i, "color", "0,0,0"), n: n(i, "count", 99)

            }

        }

        function o() {

            a = m.width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,

                c = m.height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight

        }

        function i() {

            r.clearRect(0, 0, a, c);

            var n, e, t, o, m, l;

            s.forEach(function (i, x) {

                for (i.x += i.xa, i.y += i.ya, i.xa *= i.x > a || i.x < 0 ? -1 : 1, i.ya *= i.y > c || i.y < 0 ? -1 : 1, r.fillRect(i.x - .5, i.y - .5, 1, 1), e = x + 1; e < u.length; e++) n = u[e],

                null !== n.x && null !== n.y && (o = i.x - n.x, m = i.y - n.y,

                    l = o * o + m * m, l < n.max && (n === y && l >= n.max / 2 && (i.x -= .03 * o, i.y -= .03 * m),

                    t = (n.max - l) / n.max, r.beginPath(), r.lineWidth = t / 2, r.strokeStyle = "rgba(" + d.c + "," + (t + .2) + ")", r.moveTo(i.x, i.y), r.lineTo(n.x, n.y), r.stroke()))

            }),

                x(i)

        }

        var a, c, u, m = document.createElement("canvas"),

            d = t(), l = "c_n" + d.l, r = m.getContext("2d"),

            x = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||

                function (n) {

                    window.setTimeout(n, 1e3 / 45)

                },

            w = Math.random, y = {x: null, y: null, max: 2e4};
        m.id = l, m.style.cssText = "position:fixed;top:0;left:0;z-index:" + d.z + ";opacity:" + d.o, e("body")[0].appendChild(m), o(), window.onresize = o,

            window.onmousemove = function (n) {

                n = n || window.event, y.x = n.clientX, y.y = n.clientY

            },

            window.onmouseout = function () {

                y.x = null, y.y = null

            };

        for (var s = [], f = 0; d.n > f; f++) {

            var h = w() * a, g = w() * c, v = 2 * w() - 1, p = 2 * w() - 1;
            s.push({x: h, y: g, xa: v, ya: p, max: 6e3})

        }

        u = s.concat([y]),

            setTimeout(function () {
                i()
            }, 100)

    }();
</script>
</body>
</html>